package com.bdmc.task.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.task.domain.bo.TaskTeamSpanBo;
import com.bdmc.task.domain.vo.TaskTeamSpanVo;
import com.bdmc.task.domain.TaskTeamSpan;
import com.bdmc.task.mapper.TaskTeamSpanMapper;
import com.bdmc.task.service.ITaskTeamSpanService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 考勤时间段Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-06
 */
@Service
public class TaskTeamSpanServiceImpl extends ServicePlusImpl<TaskTeamSpanMapper, TaskTeamSpan, TaskTeamSpanVo> implements ITaskTeamSpanService {

    @Override
    public TaskTeamSpanVo queryById(Integer teamSpanId){
        return getVoById(teamSpanId);
    }

    @Override
    public TableDataInfo<TaskTeamSpanVo> queryPageList(TaskTeamSpanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaskTeamSpan> lqw = buildQueryWrapper(bo);
        Page<TaskTeamSpanVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<TaskTeamSpanVo> mySelectPage(TaskTeamSpanBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<TaskTeamSpanVo> queryList(TaskTeamSpanBo bo) {
        LambdaQueryWrapper<TaskTeamSpan> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    @Override
    public List<Map<String,Object>> selectWorkingCar(TaskTeamSpanBo bo) {
        return baseMapper.selectWorkingCar(bo);
    }

    /**
     * 查询当前当班的保洁人员
     *
     * @param bo 包含deptId
     * @return list
     */
    @Override
    public List<Map<String,Object>> selectWorkingPerson(TaskTeamSpanBo bo) {
        return  baseMapper.selectWorkingPerson(bo);
    }

    private LambdaQueryWrapper<TaskTeamSpan> buildQueryWrapper(TaskTeamSpanBo bo) {
        LambdaQueryWrapper<TaskTeamSpan> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWorkTeamId() != null, TaskTeamSpan::getWorkTeamId, bo.getWorkTeamId());
        lqw.like(StringUtils.isNotBlank(bo.getTeamSpanName()), TaskTeamSpan::getTeamSpanName, bo.getTeamSpanName());
        return lqw;
    }

    @Override
    public Boolean insertByBo(TaskTeamSpanBo bo) {
        TaskTeamSpan add = BeanUtil.toBean(bo, TaskTeamSpan.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setTeamSpanId(add.getTeamSpanId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(TaskTeamSpanBo bo) {
        TaskTeamSpan update = BeanUtil.toBean(bo, TaskTeamSpan.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TaskTeamSpan entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
